<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/19 0019
 * Time: 09:36
 */

namespace app\currency\controller;


use app\currency\service\CitiesService;
use app\currency\service\CleanerService;
use app\currency\service\TimeSlotService;
use library\Controller;
use library\tools\Data;
use think\Db;

/**
 * 订单信息管理
 * Class Order
 * @package app\currency\controller
 */
class Order extends Controller
{
    protected $table = 'CleanOrder';

    /**
     * 订单列表
     * @menu true
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '订单列表';
        $where = [['status','egt','1'],['is_deleted','eq','0']];
        $this->_query($this->table)
            ->where($where)
            ->like('order_no')
            ->equal('status,pay_state,mid')
            ->dateBetween('create_at')
            ->order('create_at desc')
            ->page();
    }

    /**
     * 列表数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(array &$data)
    {
        $mids = array_unique(array_column($data, 'mid'));
        $memberList = Db::name('Member')->whereIn('id', $mids)->select();
        $cids = array_unique(array_column($data,'cid'));
        $cleanerList = Db::name('Cleaner')->whereIn('id',$cids)->select();
        $cities_ids = array_unique(array_column($data,'cities_id'));
        $cities_member_list = Db::name('CitiesMember')->whereIn('cities_id',$cities_ids)->select();
        foreach ($data as &$vo) {
            list($vo['member'], $vo['cleaner'], $vo['contact']) = [[], [], []];
            foreach ($memberList as $member) if ($member['id'] === $vo['mid']) {
                $vo['member'] = $member;
            }
            foreach ($cleanerList as $cleaner) if ($cleaner['id'] === $vo['cid']) {
                $vo['cleaner'] = $cleaner;
            }
            foreach ($cities_member_list as $cities_member) if($cities_member['cities_id'] === $vo['cities_id']) {
                $vo['contact'] = $cities_member;
            }
            $vo['address'] = CitiesService::getAddressText($vo['cities_id'],'string');
            $vo['time_slot_text'] = TimeSlotService::getTimeSlot($vo['time_slot']);
        }
    }

    /**
     * 设置保洁员
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table,'form');
    }

    /**
     * 表单数据处理
     * @param $data
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _form_filter(&$data)
    {
        if($this->request->isGet())
        {
            //查询附近的未接单的保洁员

            if($data['status'] != '1' && $data['status'] != '2')
            {
                $this->error('此订单不允许分配保洁员哦~');
            }
            if($data['service_status'] > 0)
            {
                $this->error('开始服务之后不能更换保洁员哦~');
            }
            //排除被拉黑的保洁员
            $cleaner = CleanerService::getFreeCleaner($data['service_date'],$data['time_slot'],$data['cities_center'],$data['mid']);
            $cleaner = array_map(function($v){
                $v['distance'] = number_format($v['distance']);
                return $v;
            },$cleaner);
            $this->list = $cleaner;
        } else {
            //选择保洁员修改订单信息
            if(Data::save('CleanOrder',array_merge($data,['status' => '2'])))
            {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
    }
}